package com.vy.stl.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @create: 2025/08/21 16:48
 * @description:
 */
@Data
@ToString
@NoArgsConstructor
public class Result {
    /*响应状态码*/
    private Integer code;
    /*响应消息*/
    private String msg;
    /*响应数据*/
    private Object data;

    public static Result success(Integer code) {
        return Result.success(code, null, null);
    }

    public static Result success(Integer code, String msg) {
        return Result.success(code, msg,null);
    }

    public static Result success(Integer code, Object data) {
        return Result.success(code, null, data);
    }

    /**
     * 返回成功的响应
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Result success(Integer code, String msg, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 返回错误的响应
     * @param code
     * @param msg
     * @return
     */
    public static Result error(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 将Result对象转成Json格式的字符串
     * @return
     */
    public String toJson() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(this);
    }
}
